<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Form Post URL 测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .test-section {
            margin: 20px 0;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        .url-display {
            background: #f8f9fa;
            padding: 10px;
            border-radius: 4px;
            font-family: monospace;
            font-size: 12px;
            word-break: break-all;
            margin: 10px 0;
        }
        .result {
            margin: 10px 0;
            padding: 10px;
            border-radius: 4px;
        }
        .success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Form Post URL 测试</h1>
        
        <div class="test-section">
            <h3>配置信息</h3>
            <p><strong>客户端ID:</strong> <span id="clientId">6c1f2122-b1e9-45a1-af19-ba68978278a3</span></p>
            <p><strong>租户ID:</strong> <span id="tenantId">afebf5f7-d49a-419a-8d2c-5d08baabdee1</span></p>
            <p><strong>重定向URI:</strong> <span id="redirectUri">http://localhost:8080/auth/ms-365-auth/callback</span></p>
            <p><strong>Scope:</strong> <span id="scopes">openid profile email User.Read offline_access</span></p>
        </div>
        
        <div class="test-section">
            <h3>测试操作</h3>
            <button onclick="buildFormPostUrl()">构建Form Post URL</button>
            <button onclick="testUrl()">测试URL</button>
            <button onclick="copyUrl()">复制URL</button>
        </div>
        
        <div class="test-section" id="urlSection" style="display: none;">
            <h3>生成的授权URL</h3>
            <div class="url-display" id="urlDisplay"></div>
        </div>
        
        <div class="test-section" id="resultsSection" style="display: none;">
            <h3>测试结果</h3>
            <div id="results"></div>
        </div>
    </div>

    <script>
        let currentUrl = '';
        
        function addResult(success, message) {
            const resultsDiv = document.getElementById('results');
            const resultDiv = document.createElement('div');
            resultDiv.className = `result ${success ? 'success' : 'error'}`;
            resultDiv.innerHTML = `${success ? '✅' : '❌'} ${message}`;
            resultsDiv.appendChild(resultDiv);
            document.getElementById('resultsSection').style.display = 'block';
        }
        
        function buildFormPostUrl() {
            try {
                const clientId = document.getElementById('clientId').textContent;
                const tenantId = document.getElementById('tenantId').textContent;
                const redirectUri = encodeURIComponent(document.getElementById('redirectUri').textContent);
                const scopes = encodeURIComponent(document.getElementById('scopes').textContent);
                const state = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
                const nonce = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
                
                currentUrl = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/authorize?` +
                    `client_id=${clientId}&` +
                    `scope=${scopes}&` +
                    `redirect_uri=${redirectUri}&` +
                    `response_type=code&` +
                    `response_mode=form_post&` +
                    `state=${state}&` +
                    `nonce=${nonce}&` +
                    `prompt=login&` +
                    `client_info=1`;
                
                document.getElementById('urlDisplay').textContent = currentUrl;
                document.getElementById('urlSection').style.display = 'block';
                
                addResult(true, 'Form Post URL构建成功');
                addResult(true, `URL包含response_mode=form_post: ${currentUrl.includes('response_mode=form_post')}`);
                addResult(true, `URL长度: ${currentUrl.length} 字符`);
                
            } catch (error) {
                addResult(false, `URL构建失败: ${error.message}`);
            }
        }
        
        function testUrl() {
            if (!currentUrl) {
                addResult(false, '请先构建URL');
                return;
            }
            
            addResult(true, '开始测试URL...');
            
            // 检查URL参数
            const urlParams = new URLSearchParams(currentUrl.split('?')[1]);
            const responseMode = urlParams.get('response_mode');
            const responseType = urlParams.get('response_type');
            const clientId = urlParams.get('client_id');
            const scope = urlParams.get('scope');
            
            addResult(responseMode === 'form_post', `响应模式: ${responseMode}`);
            addResult(responseType === 'code', `响应类型: ${responseType}`);
            addResult(!!clientId, `客户端ID: ${clientId ? '已设置' : '未设置'}`);
            addResult(!!scope, `Scope: ${scope ? '已设置' : '未设置'}`);
            
            addResult(true, 'URL参数检查完成');
        }
        
        function copyUrl() {
            if (!currentUrl) {
                addResult(false, '请先构建URL');
                return;
            }
            
            navigator.clipboard.writeText(currentUrl).then(() => {
                addResult(true, 'URL已复制到剪贴板');
            }).catch(() => {
                addResult(false, '复制URL失败');
            });
        }
        
        // 页面加载时自动构建URL
        window.onload = function() {
            setTimeout(buildFormPostUrl, 500);
        };
    </script>
</body>
</html> 